import { timeTransform } from '@/utils/time';
import {
  getNongjizulinListService,
  deletePostService,
  addPostService,
} from '@/services/nongjizulin';
import { notiErr, notiSuccess } from '@/utils/notification';


const namespace = 'jixiexinxi_nongjizulin';
const initialState = {
  list: [
    // {
    //   id: 1,
    //   machineDesc: '收割机', // 农机型号
    //   contact: '13451189097', // 联系方式
    //   price: 1357.88, // price
    //   time: '2019-03-22', // price
    //   isMyPost: false,
    // },
  ],
};

export default {
  namespace,

  state: {
    ...initialState,
  },

  effects: {
    * getNongjizulinListEffect(_, { call, put, select }) {
      const token = yield select(({ app }) => app.user.token);
      const response = yield call(getNongjizulinListService, { token });

      if (response && !response.errno && response.data) {
        // 登录成功之后的操作
        const rdata = response.data;
        yield put({
          type: 'getNongjizulinListReducer',
          payload: {
            list: rdata.map(v => ({
              id: v.id,
              machineDesc: v.machine_desc, // 农机型号
              contact: v.contact, // 联系方式
              price: v.price, // price
              time: v.submit_date, // price
              isMyPost: v.isMine,
            }))
          },
        });
      } else {
        notiErr(response.errmsg);
      }
    },
    // 新增一条记录
    * addOneRecordEffect({ payload: { data } }, { call, put, select }) {
      const date = timeTransform(Math.floor(Date.now() / 1000), 'YYYY-MM-DD');
      const record = {
        machineDesc: data.machineDesc, // 农机型号
        contact: data.contact, // 联系方式
        price: data.price, // price
        time: date,
      };

      const token = yield select(({ app }) => app.user.token);
      const response = yield call(addPostService, {
        token,
        ...record,
        submitDate: date,
      });

      if (response && !response.errno && response.data) {
        // 登录成功之后的操作
        const rdata = response.data;
        const newRecord =   {
          id: rdata.id,
          ...record,
        };
        yield put({
          type: 'addOneRecordReducer',
          payload: {
            newRecord: {
              ...newRecord,
              isMyPost: true,
            },
          },
        });
      } else {
        notiErr(response.errmsg);
      }
    },
    * deleteOneRecordEffect({ payload: { data } }, { call, put, select }) {

      const token = yield select(({ app }) => app.user.token);
      const response = yield call(deletePostService, {
        token,
        recordId: data.id,
      });

      if (response && !response.errno && response.data) {
        yield put({
          type: 'deleteOneRecordReducer',
          payload: {
            id: data.id,
          },
        });
      } else {
        notiErr(response.errmsg);
      }
    },
  },

  reducers: {
    // 新增一条记录
    addOneRecordReducer(state, { payload }) {
      return {
        ...state,
        list: [payload.newRecord, ...state.list],
      };
    },
    // 删除一条记录
    deleteOneRecordReducer(state, { payload }) {
      return {
        ...state,
        list: state.list.filter(v => v.id !== payload.id),
      };
    },
    getNongjizulinListReducer(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    resetDataReducer() {
      return {
        ...initialState,
      };
    },
  },
};
